package stud.mpgame.flash.gui;

import flash.automation.MouseAutomationAction;
import flash.events.ActivityEvent;
import org.aswing.JButton;
import org.aswing.JFrame;
import org.aswing.geom.IntDimension;
import org.aswing.AsWingManager;
import org.aswing.AsWingConstants;
import org.aswing.JLabel;
import org.aswing.JTextComponent;
import org.aswing.JTextField;
import flash.events.MouseEvent;

import flash.Lib;
import flash.display.Sprite;
import flash.display.StageScaleMode;

class ConnectFrame extends Sprite {
	
	private var connectionFun: String->String->String->String->Void;
	
	private var hostText : JTextField;
	private var portText : JTextField;
	private var userIDText : JTextField;
	private var loginText : JTextField;
	
	private var frame : JFrame;
	
	
	private static var wFrame : Int = 220;
	private static var hFrame : Int = 200;
	
	private static var wLabel : Int = 55;
	private static var hLabel : Int = 25;
	
	private static var wTextField : Int = 135;
	private static var hTextField : Int = hLabel;
	
	private static var wButton : Int = wTextField;
	private static var hButton : Int = hTextField;
	
	private static var Xlocation : Int = 0;
	private static var Ylocation : Int = 5;
	
	private static var Xspace : Int = 45; //отступы
	private static var Yspace : Int = 30; //отступы
	
	private static var YlocationButton : Int = 130; //отступы

	   
    public function new(func : String->String->String->String->Void ) {
        super();
		this.frame = new JFrame( this, "Registration");
		connectionFun = func;
		createFrame();
	}
	
	
	private function onClick (e : MouseEvent) {
	    if (connectionFun != null ){
		    connectionFun(hostText.getText(), portText.getText(), userIDText.getText(), loginText.getText());
		}
		else trace("error: onClick function of connectionButton is null");	
		
	}
	
	public function setConnectFunction(func:String->String->String->String->Void):Void {
	    connectionFun = func;
		
	}
	
	private function createFrame():Void {	
    //reset ui to its defaults
    AsWingManager.initAsStandard(flash.Lib.current);
        //create a frame  
        //var frame : JFrame = new JFrame( this, "Registration");
		frame.setClosable(false); //don't close
		frame.setResizable(false);
		trace("sdsds");
        //if you want to create a "fullscreen" interface rather than
        //draggable window with title, use JWindow instead
        
        frame.setSize(new IntDimension( wFrame, hFrame ) );
   
	     var host = new JLabel("host: ");
	          host.setSize(new IntDimension( wLabel, hLabel ) );
			  host.setLocationXY(Xlocation, Ylocation);
			  host.setHorizontalAlignment(AsWingConstants.LEFT);
			  
			  //host.setHorizontalTextPosition(15);
			 // host.setHorizontalTextPosition(AsWingConstants.LEFT);
			 // host.setVerticalTextPosition(AsWingConstants.LEFT);
	          frame.getContentPane().addChild(host);
		  
		    hostText = new JTextField("127.0.0.1");
		    hostText.setLocationXY(Xlocation + Xspace, Ylocation);
	        hostText.setSize(new IntDimension( wTextField, hTextField ));
			frame.getContentPane().addChild(hostText);
			  
		var port = new JLabel("port: ");
	          port.setSize(new IntDimension( wLabel, hLabel ) );
			  port.setHorizontalAlignment(AsWingConstants.LEFT);
			  port.setLocationXY(Xlocation, Ylocation + Yspace);
			 // port.setHorizontalTextPosition(15);
	          frame.getContentPane().addChild(port);
		  
		    portText = new JTextField("8090");
		    portText.setLocationXY(Xlocation + Xspace, Ylocation + Yspace);
	        portText.setSize(new IntDimension( wTextField, hTextField ));
			frame.getContentPane().addChild(portText);
			  
        var userID = new JLabel("userID: ");
	          userID.setSize(new IntDimension( wLabel, hLabel ) );
			  userID.setHorizontalAlignment(AsWingConstants.LEFT);
			  //userID.setHorizontalTextPosition(AsWingConstants.LEFT);
			  userID.setLocationXY(Xlocation, Ylocation + 2*Yspace);
	          frame.getContentPane().addChild(userID);
		  
		     userIDText = new JTextField("1");
		    userIDText.setLocationXY(Xlocation + Xspace, Ylocation + 2*Yspace);
	        userIDText.setSize(new IntDimension( wTextField, hTextField ));
			frame.getContentPane().addChild(userIDText);
		
		var login = new JLabel("login: ");
	          login.setSize(new IntDimension( wLabel, hLabel ) );
			  login.setHorizontalAlignment(AsWingConstants.LEFT);
			  login.setLocationXY(Xlocation, Ylocation + 3*Yspace);
			  //login.setHorizontalTextPosition(AsWingConstants.LEFT);
	          frame.getContentPane().addChild(login);
		  
		    loginText = new JTextField("testuser");
		    loginText.setLocationXY(Xlocation + Xspace, Ylocation + 3*Yspace);
	        loginText.setSize(new IntDimension( wTextField, hTextField ));
			frame.getContentPane().addChild(loginText);
			
			
		var buttonConnect = new JButton("connect");
			buttonConnect.setLocationXY(Xlocation + Xspace, YlocationButton);
	        buttonConnect.setSize(new IntDimension( wButton, hButton ));
			
			buttonConnect.addEventListener(MouseEvent.CLICK, onClick);
			
			frame.getContentPane().addChild(buttonConnect);

        //add the frame to the stage
        Lib.current.stage.addChild(this);
        this.stage.scaleMode = StageScaleMode.NO_SCALE;
        
        //show it!
        frame.show();
	}
	
	public function hide():Void {
		frame.hide();
	}
	
}